import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Query,
} from "@nestjs/common";
import { CurrentUserId } from "../common/decorators/user.decorator";
import { CreateEventDto } from "./dto/create-event.dto";
import { EventsService } from "./events.service";

@Controller("events")
export class EventsController {
  constructor(private readonly eventsService: EventsService) {}

  @Post("create")
  create(
    @CurrentUserId() userId: number,
    @Body() createEventDto: CreateEventDto
  ) {
    return this.eventsService.create({ ...createEventDto, userId });
  }

  @Get("page")
  async findAll(
    @CurrentUserId() userId: number,
    @Query("page") page: number = 1,
    @Query("size") size: number = 10,
    @Query("eventName") eventName?: string,
    @Query("eventReson") eventReson?: string
  ) {
    // 确保 page 和 size 是正整数
    page = Math.max(1, Number(page));
    size = Math.max(1, Math.min(100, Number(size))); // 限制最大每页100条

    return this.eventsService.findUserEventsWithPagination(userId, page, size, {
      eventName,
      eventReson,
    });
  }

  @Get("all")
  async findAllEvents(
    @Query("page") page: number = 1,
    @Query("limit") limit: number = 10,
    @Query("search") search?: string // 添加搜索查询参数
  ) {
    // 确保 page 和 limit 是正整数
    page = Math.max(1, Number(page));
    limit = Math.max(1, Math.min(100, Number(limit))); // 限制最大每页100条

    return this.eventsService.findAllWithPagination(page, limit, search);
  }

  @Get(":id")
  findOne(@Param("id") id: number) {
    return this.eventsService.findOne(id);
  }

  @Delete(":id")
  remove(@Param("id") id: string) {
    return this.eventsService.remove(+id);
  }
}
